package com.dinzeer.godfox.effect;

import com.dinzeer.godfox.entity.GoldenDrive;
import com.dinzeer.godfox.regsiter.GBEffectRegsitry;
import mods.flammpfeil.slashblade.SlashBlade;
import mods.flammpfeil.slashblade.capability.concentrationrank.ConcentrationRankCapabilityProvider;
import mods.flammpfeil.slashblade.event.SlashBladeEvent;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.util.KnockBacks;
import mods.flammpfeil.slashblade.util.VectorHelper;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.awt.*;

@Mod.EventBusSubscriber
public class GoldMonth extends MobEffect {

    // 配置常量

    private static final int DEFAULT_LIFETIME = 100;
    private static final float DEFAULT_SPEED = 3.0f;
    private static final float EYE_HEIGHT_MULTIPLIER = 0.75f;
    private static final float INITIAL_OFFSET = 0.3f;

    // 视角旋转常量
    private static final float PITCH_ROTATION = -90.0f;
    private static final float YAW_ROTATION = 90.0f;

    public GoldMonth() {
        super(MobEffectCategory.BENEFICIAL, Color.YELLOW.getRGB());
    }

    @SubscribeEvent
    public static void onSlashAttack(SlashBladeEvent.DoSlashEvent event) {
        LivingEntity user = event.getUser();

        if (user.hasEffect(GBEffectRegsitry.GoldMonth.get())) {
            int amplifier = user.getEffect(GBEffectRegsitry.GoldMonth.get()).getAmplifier();
            double damage = event.getDamage() * (amplifier+1);

            createGoldenDriveAttack(user, damage,event.getRoll());
        }
    }


    private static void createGoldenDriveAttack(LivingEntity player, double damage, float roll) {
        doSlash(player, roll, DEFAULT_LIFETIME, Vec3.ZERO, false, damage, DEFAULT_SPEED);
    }





    public static GoldenDrive doSlash(LivingEntity player, float roll, int lifetime, Vec3 centerOffset,
                                      boolean critical, double damage, float speed) {
        // 客户端不执行
        if (player.level().isClientSide()) {
            return null;
        }

        Vec3 spawnPosition = calculateSpawnPosition(player, centerOffset);
        GoldenDrive drive = createGoldenDrive(player, spawnPosition, damage, speed, 14276889);

        configureGoldenDrive(drive, player, roll,  0.0F, lifetime, critical, null);

        player.level().addFreshEntity(drive);
        return drive;
    }



    /**
     * 计算生成位置
     */
    private static Vec3 calculateSpawnPosition(LivingEntity player, Vec3 centerOffset) {
        Vec3 lookAngle = player.getLookAngle();
        Vec3 basePos = player.position()
                .add(0.0, player.getEyeHeight() * EYE_HEIGHT_MULTIPLIER, 0.0)
                .add(lookAngle.scale(INITIAL_OFFSET));

        // 应用中心偏移
        Vec3 yOffset = VectorHelper.getVectorForRotation(PITCH_ROTATION, player.getViewYRot(0.0F))
                .scale(centerOffset.y);
        Vec3 zOffset = VectorHelper.getVectorForRotation(0.0F, player.getViewYRot(0.0F) + YAW_ROTATION)
                .scale(centerOffset.z);
        Vec3 xOffset = lookAngle.scale(centerOffset.z); // 注意：这里可能是笔误，应该是centerOffset.x

        return basePos.add(yOffset).add(zOffset).add(xOffset);
    }

    /**
     * 创建GoldenDrive实体
     */
    private static GoldenDrive createGoldenDrive(LivingEntity player, Vec3 position, double damage, float speed, int colorCode) {
        GoldenDrive drive = new GoldenDrive(SlashBlade.RegistryEvents.Drive, player.level());
        drive.setPos(position.x, position.y, position.z);
        drive.setDamage(damage);
        drive.setSpeed(speed);
        drive.setColor(colorCode);
        drive.setOwner(player);

        return drive;
    }

    /**
     * 配置GoldenDrive属性
     */
    private static void configureGoldenDrive(GoldenDrive drive, LivingEntity player, float roll, float yRot,
                                             int lifetime, boolean critical, KnockBacks knockback) {
        // 设置射击方向
        Vec3 lookAngle = player.getLookAngle();
        Vec3 shootAngle = lookAngle.yRot(yRot);
        drive.shoot(shootAngle.x, shootAngle.y, shootAngle.z, drive.getSpeed(), 0.0F);

        // 设置其他属性
        drive.setRotationRoll(roll);
        drive.setIsCritical(critical);
        drive.setKnockBack(knockback);
        drive.setLifetime((float) lifetime);

        // 设置集中等级
        setConcentrationRank(drive, player);
    }

    /**
     * 设置集中等级
     */
    private static void setConcentrationRank(GoldenDrive drive, LivingEntity player) {
        player.getCapability(ConcentrationRankCapabilityProvider.RANK_POINT).ifPresent(rank -> {
            long gameTime = player.level().getGameTime();
            drive.setRank(rank.getRankLevel(gameTime));
        });
    }
}